<?php

App::import('Vendor', 'Sociable.Facebook');
App::import('Model','Sociable.SocialAccount');
class FacebookAccount extends SocialAccount
{
	var $name = 'FacebookAccount';
	//var $apiStatusMethod = 'stream_publish';
	
	var $fbStatusName = 'publish_stream';
	
	function getSessionUser()
	{
		return $this->getApi()->getUser();
	}
	
	function hasPermission($entityId, $name)
	{
		$return = false;
		$facebookId = $this->field('facebook_id',array($this->entityKey => $entityId));
		try 
		{
			if($facebookId)
			{
				$return = $this->getApi()->api_client->users_hasAppPermission($name,$facebookId);
			}
		}
		catch(Exception $e)
		{
			$return = false;
		}
		return $return;
	}
	/**
		
	**/
	function saveFbSession($entity)
	{
		
		$facebookUserId = $this->getApi()->get_loggedin_user();

		if($entity && $facebookUserId)
		{
			$sessionKey = $this->getApi()->api_client->session_key;
			$data[$this->alias] = array($this->entityKey=>$entity,'session_key'=>$sessionKey,'facebook_id'=>$facebookUserId);
			$this->deleteAll(array($this->entityKey=>$entity));
			$this->create();
			$this->save($data);
			return true;
 		}
		return false;
	}
	
	function beforeApiStatusUpdate($accountInfo,$message)
	{
		$return = false;
		if($this->hasPermission($accountInfo[$this->entityKey],$this->fbStatusName))
		{
			$return = true;
			
		}
		if($return === true && is_array($message) == false)
		{
			$message  = array('message'=>$message);  
		}
		if($return === true)
		{
			return parent::beforeApiStatusUpdate($accountInfo,$message);
		}
		return false;
	}
}

?>